﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.IO;

namespace Chapter02_SuggestedPractice01
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (DriveInfo drive in DriveInfo.GetDrives())
                {
                    TreeViewItem item = new TreeViewItem();
                    item.Header = drive.Name;

                    if (drive.IsReady)
                    {
                        DirectoryInfo dir = new DirectoryInfo(drive.Name);

                        foreach (DirectoryInfo directory in dir.GetDirectories())
                        {
                            TreeViewItem itemDir = new TreeViewItem();
                            itemDir.Header = directory.Name;
                            item.Items.Add(itemDir);

                            foreach (FileInfo file in directory.GetFiles())
                            {
                                TreeViewItem itemFile = new TreeViewItem();
                                itemFile.Header = file.Name;
                                itemDir.Items.Add(itemFile);
                            }
                        }
                    }


                    this.treeView1.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
